package com.lzm.back.controller;

import com.lzm.back.common.MyResult;
import com.lzm.back.entity.User;
import com.lzm.back.exception.CustomeException;
import com.lzm.back.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WebController {

    @Resource
    private UserService userService;

    @GetMapping("/hello")
    public MyResult hello() {

        throw new CustomeException("400", "fail");
    }


    //登录
    @PostMapping("/login")
    public MyResult login(@RequestBody User User) {

        com.lzm.back.entity.User dbUser = userService.login(User);

        return MyResult.success(dbUser);

    }


    //注册
    @PostMapping("/register")
    public ResponseEntity<MyResult> register(@RequestBody User User) {
        try {
            userService.register(User);
            return ResponseEntity.ok(MyResult.success());
        } catch (CustomeException e) {
            return ResponseEntity.status(HttpStatus.CONFLICT)
                    .body(MyResult.error(e.getNode(), e.getMessage()));
        }
    }


}
